import os
import cgi

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Newsletter(db.Model):
  email = db.StringProperty()

class MainPage(webapp.RequestHandler):
  def get(self):
	template_values = {}
	path = os.path.join(os.path.dirname(__file__), 'index.html')
	self.response.out.write(template.render(path, template_values))
  def post(self):
	#self.response.out.write(self.request.get('email'))
	
	newsletter = Newsletter()
	newsletter.email = self.request.get('email')
	newsletter.put()
	
	newsletters = db.GqlQuery("SELECT * FROM Newsletter")
	for itm in newsletters:
	  self.response.out.write(cgi.escape(itm.email))
	
	#newsletter = Newsletter()
	#newsletter.email = 'lars' #self.request.get('email')
	#newsletter.put()
	#self.redirect('/')

application = webapp.WSGIApplication([('/', MainPage)], debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()